package com.smart.controller.sys;

import java.text.ParseException;
import java.util.Map;
import java.util.List;
import java.util.HashMap;

import com.smart.dto.AttendanceStatus;
import com.smart.util.Page;
import java.util.ArrayList;

import com.smart.util.ExportExcelUtils;
import com.smart.util.Messages;
import com.smart.util.Pageable;
import javax.annotation.Resource;
import lombok.AllArgsConstructor;
import io.swagger.annotations.Api;
import com.smart.entity.SsAttendance;

import io.swagger.annotations.ApiOperation;
import com.smart.controller.BaseController;
import com.smart.service.AttendanceService;
import javax.servlet.http.HttpServletResponse;
import com.smart.vo.dto.WebSsAttendanceExcelDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020年6月10日 下午7:30:21
 * Copyright (C) 深圳工勘科技
 * @说明: 考勤管理
 **/
@AllArgsConstructor
@RestController("sysAttendanceController")
@RequestMapping("/sys/attendance")
@Api(value="attendance",tags="考勤管理")
public class AttendanceController extends BaseController{

	@Resource
	private AttendanceService attendanceService;
	
	/**
	 * @param pageable
	 * @return 分页查询数据
	 **/
	@GetMapping(value="/page")
	@ApiOperation(value="Sys=>获取巡查历史列表",notes="Sys获取巡查记录列表")
	public Messages<Page<SsAttendance>> page(
			@RequestParam(name="name")String name,
			@RequestParam(name="identity")String identity,
			@RequestParam(name="createDate_begin")String createDate_begin,
			@RequestParam(name="createDate_end")String createDate_end,
			Pageable pageable){
		Map<String,String> search = new HashMap<>();
		search.put("name", name);
		search.put("identity", identity);
		search.put("createDate_begin", createDate_begin);
		search.put("createDate_end", createDate_end);
		pageable.setSearch(search);
		return Messages.success(attendanceService.findPage(pageable));
	}

	@GetMapping(value="/statistical")
	@ApiOperation(value="获取考勤统计",notes = "获取考勤")
	public Messages<List<AttendanceStatus>> statistical(
			@RequestParam(name="userId") Integer userId,
			@RequestParam(name="date")String date){
		List<AttendanceStatus> dates= null;
		try {
			dates = attendanceService.findAttenByUser(userId,date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return Messages.success(dates);
	}
	
	/**
	 * @param response 
	 * @return 导出excel数据
	 **/
	@GetMapping(value="/export_excel")
    public void exportExcel(HttpServletResponse response){
		List<SsAttendance> list=attendanceService.findAll();
		List<WebSsAttendanceExcelDTO> attendanceList = new ArrayList<>();
		if(list != null){
			for(SsAttendance s:list){
				WebSsAttendanceExcelDTO dto=new WebSsAttendanceExcelDTO();
				dto.setCreateDate(s.getCreateDate());
				dto.setCreateTime(s.getCreateTime());
				dto.setIdentity(s.getIdentity());
				dto.setLocation(s.getLocation());
				dto.setNickName(s.getNickName());
				dto.setPhone(s.getPhone());
				attendanceList.add(dto);
			}
		}
		String title = "考勤记录.xlsx";
        String[] rowName={"日期","姓名","手机号","身份","打卡时间","打卡地点"};
        List<Object> datalist = new ArrayList<>(1);
        datalist.addAll(attendanceList);
        ExportExcelUtils.exportExcel(response, title, rowName, datalist);
	}
	
	
}